﻿using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.ProcessTasks;
using MVC系统mvc.Dto.ProcessTasksDto;
using MVC系统mvc.Dto.WorkerListDto;
using Newtonsoft.Json;
using NuGet.Common;
using WMS_MAV;

namespace MVC系统mvc.Controllers
{
    /// <summary>
    /// 工序任务控制器
    /// </summary>
    public class ProcessTasksController : Controller
    {
        /// <summary>
        /// 配置信息
        /// </summary>
        protected readonly IConfiguration configuration;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration">配置信息</param>
        public ProcessTasksController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 工序任务列表--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowProcessTasks()
        {
            return View();
        }

        /// <summary>
        /// 添加工序任务列表--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddProcessTasks()
        {
            return View();
        }

        /// <summary>
        /// 生产执行--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ManufacturingExecution(int id)
        {
            ViewBag.TaskId = id; // 将任务 ID 存储在 ViewBag 中
            return View();
        }

        /// <summary>
        /// 报工记录--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowReportingList()
        {
            return View();
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        private string ExtractTokenFromHeader()
        {
            if (Request.Headers.TryGetValue("Authorization", out var tokenHeaderValues) && tokenHeaderValues.Count > 0)
            {
                string token = tokenHeaderValues.FirstOrDefault()?.TrimStart("Bearer ".ToCharArray());
                return !string.IsNullOrEmpty(token) ? token : null;
            }
            return null;
        }

        /// <summary>
        /// 添加工序任务
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> AddProcessTasksInfo(AddProcessTasksDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/CreadProcessTasks";
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }

        /// <summary>
        /// 工序任务数据分页展示列表
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetProcessTasksList(ShowProcessTasksDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/ShowShowProcessTasksList?TaskNumber=" + dto.TaskNumber + "&TaskStatus=" + dto.TaskStatus + "&PageIndex=" + dto.PageIndex + "&PageSize=" + dto.PageSize;
            var data = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 删除工序任务信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> DeletedProcessTasks(DeletedProcessTasksDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/DeleteProcessTasks";
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }

        /// <summary>
        /// 批量删除工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> BatchDeletedProcessTaskss(BatchDeleteProcessTasksDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/BatchDeletedProcessTasks";
            //var data = "{\"ids\":" + dto.ids +" , \"deletedBy\":" + dto.deletedBy + "} ";
            //var data = new StringContent(JsonSerializer.Serialize(new { ids = dto.ids, deletedBy = dto.deletedBy }), Encoding.UTF8, "application/json");
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }


        /// <summary>
        /// 反填工序任务信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> ReversedProcessTasks(ReverseFillingProcessTasksDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/ReverseFillingProcessTasks?ProcessTasksId=" + dto.ProcessTasksId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 修改工序任务信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> UpdatedProcessTasks(UpdateProcessTasksDto objTwo, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/UpdateProcessTasks";
            string list = JsonConvert.SerializeObject(objTwo);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

        /// <summary>
        /// 修改工序任务状态
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> UpdateProcessTasksStatus(UpdateProcessTasksStatusDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/UpdateProcessTasksStatus";
            string list = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

        /// <summary>
        /// 添加报工信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> AddReportingInfo(AddReportingDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/CreadReporting";
            string list = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

        /// <summary>
        /// 报工记录分页展示列表
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetReportingList(ShowReportingDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/ShowAllReportingList?ReportingNumber=" + dto.ReportingNumber + "&TaskNumber=" + dto.TaskNumber + "&PageIndex=" + dto.PageIndex + "&PageSize=" + dto.PageSize;
            var data = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 反填工人信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> ReverseReporting(ReverseFillingReportingDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "ppapi/api/ProcessTasks/ReverseFillingReporting?ReportingId=" + dto.ReportingId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

    }
}
